package com.zzyl.controller.security;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色分页查询
     * @param pageNum
     * @param pageSize
     * @param dto
     * @return
     */
    @ApiOperation("角色分页")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult pageQuery(@PathVariable Integer pageNum,
                                    @PathVariable Integer pageSize,
                                    @RequestBody RoleDto dto){
        log.info("角色分页:{} {} {}",pageNum,pageSize,dto);
        PageBean<RoleVo> pageBean = roleService.pageQuery(pageNum,pageSize,dto);
        return ResponseResult.success(pageBean);

    }

    /**
     * 角色添加
     */
    @ApiOperation("角色添加")
    @PutMapping()
    public ResponseResult addRole(@RequestBody RoleDto dto){
        log.info("角色添加:{}",dto);
        roleService.addRole(dto);
        return ResponseResult.success();
    }

    /**
     * 根据角色查询选中的资源数据
     * @return
     */
    @ApiOperation("根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult getById(@PathVariable Long roleId){
        Set<String> list = roleService.getById(roleId);
        return ResponseResult.success(list);
    }

    /**
     * 角色修改
     * @param dto
     * @return
     */
    @ApiOperation("角色修改")
    @PatchMapping()
    public ResponseResult updateRole(@RequestBody RoleDto dto){
        log.info("角色修改:{}",dto);
        roleService.updateRole(dto);
        return ResponseResult.success();
    }

    /**
     * 根据Id删除角色

     * @return
     */
    @ApiOperation("删除角色")
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable Long id){
        roleService.deleteById(id);
        return ResponseResult.success();
    }

    @ApiOperation("查询所有角色")
    @PostMapping("/init-roles")
    public ResponseResult getAll(){
        List<RoleVo> list = roleService.getAll();
        return ResponseResult.success(list);
    }
}
